/*
 * ================================
 * eli960@163.com
 * http://www.mailhonor.com/
 * 2015-12-05
 * ================================
 */

#include "zc.h"
#include <stdint.h>

unsigned long zcrc64(const void *data, int size, unsigned long init_value)
{
    int i;
    unsigned long crc = init_value;
    static unsigned long crc64_tab[256] = {
        0x0000000000000000UL, 0x7ad870c830358979UL,
        0xf5b0e190606b12f2UL, 0x8f689158505e9b8bUL,
        0xc038e5739841b68fUL, 0xbae095bba8743ff6UL,
        0x358804e3f82aa47dUL, 0x4f50742bc81f2d04UL,
        0xab28ecb46814fe75UL, 0xd1f09c7c5821770cUL,
        0x5e980d24087fec87UL, 0x24407dec384a65feUL,
        0x6b1009c7f05548faUL, 0x11c8790fc060c183UL,
        0x9ea0e857903e5a08UL, 0xe478989fa00bd371UL,
        0x7d08ff3b88be6f81UL, 0x07d08ff3b88be6f8UL,
        0x88b81eabe8d57d73UL, 0xf2606e63d8e0f40aUL,
        0xbd301a4810ffd90eUL, 0xc7e86a8020ca5077UL,
        0x4880fbd87094cbfcUL, 0x32588b1040a14285UL,
        0xd620138fe0aa91f4UL, 0xacf86347d09f188dUL,
        0x2390f21f80c18306UL, 0x594882d7b0f40a7fUL,
        0x1618f6fc78eb277bUL, 0x6cc0863448deae02UL,
        0xe3a8176c18803589UL, 0x997067a428b5bcf0UL,
        0xfa11fe77117cdf02UL, 0x80c98ebf2149567bUL,
        0x0fa11fe77117cdf0UL, 0x75796f2f41224489UL,
        0x3a291b04893d698dUL, 0x40f16bccb908e0f4UL,
        0xcf99fa94e9567b7fUL, 0xb5418a5cd963f206UL,
        0x513912c379682177UL, 0x2be1620b495da80eUL,
        0xa489f35319033385UL, 0xde51839b2936bafcUL,
        0x9101f7b0e12997f8UL, 0xebd98778d11c1e81UL,
        0x64b116208142850aUL, 0x1e6966e8b1770c73UL,
        0x8719014c99c2b083UL, 0xfdc17184a9f739faUL,
        0x72a9e0dcf9a9a271UL, 0x08719014c99c2b08UL,
        0x4721e43f0183060cUL, 0x3df994f731b68f75UL,
        0xb29105af61e814feUL, 0xc849756751dd9d87UL,
        0x2c31edf8f1d64ef6UL, 0x56e99d30c1e3c78fUL,
        0xd9810c6891bd5c04UL, 0xa3597ca0a188d57dUL,
        0xec09088b6997f879UL, 0x96d1784359a27100UL,
        0x19b9e91b09fcea8bUL, 0x636199d339c963f2UL,
        0xdf7adabd7a6e2d6fUL, 0xa5a2aa754a5ba416UL,
        0x2aca3b2d1a053f9dUL, 0x50124be52a30b6e4UL,
        0x1f423fcee22f9be0UL, 0x659a4f06d21a1299UL,
        0xeaf2de5e82448912UL, 0x902aae96b271006bUL,
        0x74523609127ad31aUL, 0x0e8a46c1224f5a63UL,
        0x81e2d7997211c1e8UL, 0xfb3aa75142244891UL,
        0xb46ad37a8a3b6595UL, 0xceb2a3b2ba0eececUL,
        0x41da32eaea507767UL, 0x3b024222da65fe1eUL,
        0xa2722586f2d042eeUL, 0xd8aa554ec2e5cb97UL,
        0x57c2c41692bb501cUL, 0x2d1ab4dea28ed965UL,
        0x624ac0f56a91f461UL, 0x1892b03d5aa47d18UL,
        0x97fa21650afae693UL, 0xed2251ad3acf6feaUL,
        0x095ac9329ac4bc9bUL, 0x7382b9faaaf135e2UL,
        0xfcea28a2faafae69UL, 0x8632586aca9a2710UL,
        0xc9622c4102850a14UL, 0xb3ba5c8932b0836dUL,
        0x3cd2cdd162ee18e6UL, 0x460abd1952db919fUL,
        0x256b24ca6b12f26dUL, 0x5fb354025b277b14UL,
        0xd0dbc55a0b79e09fUL, 0xaa03b5923b4c69e6UL,
        0xe553c1b9f35344e2UL, 0x9f8bb171c366cd9bUL,
        0x10e3202993385610UL, 0x6a3b50e1a30ddf69UL,
        0x8e43c87e03060c18UL, 0xf49bb8b633338561UL,
        0x7bf329ee636d1eeaUL, 0x012b592653589793UL,
        0x4e7b2d0d9b47ba97UL, 0x34a35dc5ab7233eeUL,
        0xbbcbcc9dfb2ca865UL, 0xc113bc55cb19211cUL,
        0x5863dbf1e3ac9decUL, 0x22bbab39d3991495UL,
        0xadd33a6183c78f1eUL, 0xd70b4aa9b3f20667UL,
        0x985b3e827bed2b63UL, 0xe2834e4a4bd8a21aUL,
        0x6debdf121b863991UL, 0x1733afda2bb3b0e8UL,
        0xf34b37458bb86399UL, 0x8993478dbb8deae0UL,
        0x06fbd6d5ebd3716bUL, 0x7c23a61ddbe6f812UL,
        0x3373d23613f9d516UL, 0x49aba2fe23cc5c6fUL,
        0xc6c333a67392c7e4UL, 0xbc1b436e43a74e9dUL,
        0x95ac9329ac4bc9b5UL, 0xef74e3e19c7e40ccUL,
        0x601c72b9cc20db47UL, 0x1ac40271fc15523eUL,
        0x5594765a340a7f3aUL, 0x2f4c0692043ff643UL,
        0xa02497ca54616dc8UL, 0xdafce7026454e4b1UL,
        0x3e847f9dc45f37c0UL, 0x445c0f55f46abeb9UL,
        0xcb349e0da4342532UL, 0xb1eceec59401ac4bUL,
        0xfebc9aee5c1e814fUL, 0x8464ea266c2b0836UL,
        0x0b0c7b7e3c7593bdUL, 0x71d40bb60c401ac4UL,
        0xe8a46c1224f5a634UL, 0x927c1cda14c02f4dUL,
        0x1d148d82449eb4c6UL, 0x67ccfd4a74ab3dbfUL,
        0x289c8961bcb410bbUL, 0x5244f9a98c8199c2UL,
        0xdd2c68f1dcdf0249UL, 0xa7f41839ecea8b30UL,
        0x438c80a64ce15841UL, 0x3954f06e7cd4d138UL,
        0xb63c61362c8a4ab3UL, 0xcce411fe1cbfc3caUL,
        0x83b465d5d4a0eeceUL, 0xf96c151de49567b7UL,
        0x76048445b4cbfc3cUL, 0x0cdcf48d84fe7545UL,
        0x6fbd6d5ebd3716b7UL, 0x15651d968d029fceUL,
        0x9a0d8ccedd5c0445UL, 0xe0d5fc06ed698d3cUL,
        0xaf85882d2576a038UL, 0xd55df8e515432941UL,
        0x5a3569bd451db2caUL, 0x20ed197575283bb3UL,
        0xc49581ead523e8c2UL, 0xbe4df122e51661bbUL,
        0x3125607ab548fa30UL, 0x4bfd10b2857d7349UL,
        0x04ad64994d625e4dUL, 0x7e7514517d57d734UL,
        0xf11d85092d094cbfUL, 0x8bc5f5c11d3cc5c6UL,
        0x12b5926535897936UL, 0x686de2ad05bcf04fUL,
        0xe70573f555e26bc4UL, 0x9ddd033d65d7e2bdUL,
        0xd28d7716adc8cfb9UL, 0xa85507de9dfd46c0UL,
        0x273d9686cda3dd4bUL, 0x5de5e64efd965432UL,
        0xb99d7ed15d9d8743UL, 0xc3450e196da80e3aUL,
        0x4c2d9f413df695b1UL, 0x36f5ef890dc31cc8UL,
        0x79a59ba2c5dc31ccUL, 0x037deb6af5e9b8b5UL,
        0x8c157a32a5b7233eUL, 0xf6cd0afa9582aa47UL,
        0x4ad64994d625e4daUL, 0x300e395ce6106da3UL,
        0xbf66a804b64ef628UL, 0xc5bed8cc867b7f51UL,
        0x8aeeace74e645255UL, 0xf036dc2f7e51db2cUL,
        0x7f5e4d772e0f40a7UL, 0x05863dbf1e3ac9deUL,
        0xe1fea520be311aafUL, 0x9b26d5e88e0493d6UL,
        0x144e44b0de5a085dUL, 0x6e963478ee6f8124UL,
        0x21c640532670ac20UL, 0x5b1e309b16452559UL,
        0xd476a1c3461bbed2UL, 0xaeaed10b762e37abUL,
        0x37deb6af5e9b8b5bUL, 0x4d06c6676eae0222UL,
        0xc26e573f3ef099a9UL, 0xb8b627f70ec510d0UL,
        0xf7e653dcc6da3dd4UL, 0x8d3e2314f6efb4adUL,
        0x0256b24ca6b12f26UL, 0x788ec2849684a65fUL,
        0x9cf65a1b368f752eUL, 0xe62e2ad306bafc57UL,
        0x6946bb8b56e467dcUL, 0x139ecb4366d1eea5UL,
        0x5ccebf68aecec3a1UL, 0x2616cfa09efb4ad8UL,
        0xa97e5ef8cea5d153UL, 0xd3a62e30fe90582aUL,
        0xb0c7b7e3c7593bd8UL, 0xca1fc72bf76cb2a1UL,
        0x45775673a732292aUL, 0x3faf26bb9707a053UL,
        0x70ff52905f188d57UL, 0x0a2722586f2d042eUL,
        0x854fb3003f739fa5UL, 0xff97c3c80f4616dcUL,
        0x1bef5b57af4dc5adUL, 0x61372b9f9f784cd4UL,
        0xee5fbac7cf26d75fUL, 0x9487ca0fff135e26UL,
        0xdbd7be24370c7322UL, 0xa10fceec0739fa5bUL,
        0x2e675fb4576761d0UL, 0x54bf2f7c6752e8a9UL,
        0xcdcf48d84fe75459UL, 0xb71738107fd2dd20UL,
        0x387fa9482f8c46abUL, 0x42a7d9801fb9cfd2UL,
        0x0df7adabd7a6e2d6UL, 0x772fdd63e7936bafUL,
        0xf8474c3bb7cdf024UL, 0x829f3cf387f8795dUL,
        0x66e7a46c27f3aa2cUL, 0x1c3fd4a417c62355UL,
        0x935745fc4798b8deUL, 0xe98f353477ad31a7UL,
        0xa6df411fbfb21ca3UL, 0xdc0731d78f8795daUL,
        0x536fa08fdfd90e51UL, 0x29b7d047efec8728UL,
    };
    unsigned char *src_c = (unsigned char *)data;

    for (i = 0; i < size; i++) {
        crc = crc64_tab[(unsigned char)crc ^ (src_c[i])] ^ (crc >> 8);
    }

    return crc;
}
